import { defineStore } from 'pinia'
import { computed, ref } from 'vue'


const useAdvStore = defineStore('adv', () => {
    //用于保存数据
    const adv = ref([
        {
            id: 1,
            name: '首页轮播名称',
            place: '首页轮播图',
            img: '',
            sort: 0,
            link: 'http://123.com',
            checked: false,
            time: '2022年11月08日'
        },
        {
            id: 2,
            name: '首页轮播名称',
            place: '首页轮播图',
            img: '',
            sort: 0,
            link: 'http://123.com',
            checked: false,
            time: '2022年11月08日'
        }, {
            id: 3,
            name: '首页轮播名称',
            place: '首页轮播图',
            img: '',
            sort: 0,
            link: 'http://123.com',
            checked: false,
            time: '2022年11月08日'
        },
    ])

    const addAdv = (obj) => {
        const iss = adv.value.find(item => item.name == obj.name)
        console.log(iss);
        if (iss) {
            iss.name = obj.name
            iss.place = obj.place
            iss.checked = obj.checked
            iss.sort = obj.sort

            location.reload()

        } else {
            adv.value.push(obj)
        }

    }

    return {
        adv,
        addAdv
    }
}, {
    persist: true
})

export default useAdvStore